package com.ziyia.androidthreadtest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    private Button changesTextBtn, toAsyncTask;
    private TextView textView;
    private final int updateText = 1;

    private class Obj extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case updateText:
                    textView.setText("无刹特斯拉");
                    break;
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();



        changesTextBtn.setOnClickListener(new View.OnClickListener() {
            Obj obj = new Obj();
            @Override
            public void onClick(View v) {
                Thread thread = new Thread(() -> {
                    Message message = new Message();
                    message.what = updateText;

                    obj.sendMessage(message);
                });
                thread.start();
            }
        });

        toAsyncTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AsyncTaskTest.class);
                startActivity(intent);
            }
        });

    }

    private void init() {
        changesTextBtn = findViewById(R.id.changeTextBtn);
        textView = findViewById(R.id.textView);
        toAsyncTask = findViewById(R.id.toAsyncTaskTest);
    }
}

